package com.qmyx.supplychain.quartz.runner;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.qmyx.supplychain.common.constants.RedisConstants;
import com.ytjj.qmyx.supplychain.common.utils.ExpressDicUtil;
import com.ytjj.qmyx.supplychain.common.utils.RedisService;
import com.ytjj.qmyx.supplychain.mapper.ExpressDicMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxExpressDic;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class CacheRunner implements ApplicationRunner {
    @Resource
    private ExpressDicMapper yxExpressDicMapper;
    @Autowired
    private RedisService redisService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 数据库读取配置文件
        List<YxExpressDic> yxExpressDicList = yxExpressDicMapper.selectAll();
        Map<String, String> map = yxExpressDicList.stream()
                .collect(Collectors.toMap(YxExpressDic::getExpressKey, YxExpressDic::getExpressValue));
        ExpressDicUtil.into(map);
        redisService.set(RedisConstants.EXPRESS_COMPANY, JSONObject.toJSONString(yxExpressDicList));
        redisService.expire(RedisConstants.EXPRESS_COMPANY, 60 * 60);
    }
}
